<?php
namespace app\api\controller;

use think\Request;

/**
 * @SWG\Swagger(
 * schemes={"http", "https"},
 * host="bike.wei-yong.com/api/Kettle",
 * consumes={"multipart/form-data"},
 * produces={"application/json"},
 * @SWG\Info(
 * version="1.0",
 * title="拓璞水壶小程序接口文档",
 * description="杨攀<br> 2017-12-26<br> 以后就在这里愉快的对接口把!<br>"
 * ),
 * 
 * @SWG\Tag(
 * name="Login",
 * description="登陆模块",
 * ),
 * @SWG\Tag(
 * name="Index",
 * description="首页模块",
 * )
 * )
 */
class Kettle extends Base
{

    /**
     * @SWG\Post(
     * path="getCustomAndBanner",
     * tags={"Index"},
     * summary="获取定制人数，Banner图",
     * description="",
     * @SWG\Parameter(name="mstr", description="加密数据", in="formData", required=true, type="string"),
     * 
     * @SWG\Response(
     * response=200,
     * description="成功后返回数据如下：",
     *  @SWG\Header(header="user_id", type="integer", description="用户ID"),
     *  @SWG\Header(header="customs", type="integer", description="定制的人数"),
     *  @SWG\Header(header="banners.goodsname", type="string", description="轮播图  - 名称"),
     *  @SWG\Header(header="banners.imgurl", type="string", description="轮播图  - 图片"),
     * )
     * )
     */
    public function getCustomAndBanner()
    {
        // 定制人数
        $customs = db('custom')->count();
        
        // banner
        $banners = db('basic')->field('goodsname, imgurl')
            ->order('id desc')
            ->limit(30)
            ->select();
        
        foreach ($banners as $key=>$value) {
            $banners[$key]['imgurl'] = 'http://topcreating.ifavine.com/' . $value['imgurl'];
        }
        
        suc('success', ['user_id'=>$this->accessData['uid'], 'customs'=>$customs, 'banners'=>$banners]);
    }

    /**
     * @SWG\Post(
     * path="getFriends",
     * tags={"Index"},
     * summary="获取分享的好友",
     * description="",
     * @SWG\Parameter(name="mstr", description="加密数据", in="formData", required=true, type="string"),
     * @SWG\Parameter(name="page", description="页码", in="formData", required=true, type="integer"),
     * @SWG\Parameter(name="pagesize", description="每页数据量", in="formData", required=true, type="integer"),
     *
     * @SWG\Response(
     * response=200,
     * description="成功后返回数据如下：",
     *  @SWG\Header(header="nickname", type="string", description="昵称"),
     *  @SWG\Header(header="avatarurl", type="string", description="头像"),
     * )
     * )
     */
    public function getFriends()
    {
        $post = request()->only(['page', 'pagesize'], 'post');
        
        $result = $this->validate($post, 'Index.page');
        
        if (true !== $result) {
            err($result);
        }
        
        // 分享好友
        $friends = db('share_log')->alias('l')
            ->join('user u', 'l.recipient_id = u.id')
            ->where(['sharer_id'=>$this->accessData['uid']])
            ->field('u.nickname, u.avatarurl')
            ->page($post['page'], $post['pagesize'])
            ->select();
        
        if (!$friends) {
            err('empty');
        }
        
        suc('success', $friends);
    }

    /**
     * @SWG\Post(
     * path="addShareLog",
     * tags={"Index"},
     * summary="增加分享记录",
     * description="",
     * @SWG\Parameter(name="mstr", description="加密数据", in="formData", required=true, type="string"),
     * @SWG\Parameter(name="sharer_id", description="分享者ID", in="formData", required=true, type="integer"),
     *
     * @SWG\Response(
     * response=200,
     * description="成功后返回数据如下：",
     * )
     * )
     */
    function addShareLog()
    {
        $post = request()->only(['sharer_id'], 'post');
        
        if (empty($post['sharer_id'])) {
            err('sharer_id empty');
        }
        
        if ($post['sharer_id'] == $this->accessData['uid']) {
            err('接收者不能是自己');
        }
        
        $insertData = [];
        $insertData['sharer_id'] = $post['sharer_id'];
        $insertData['recipient_id'] = $this->accessData['uid'];
        $insertData['create_time'] = $this->timeStamp;
        
        try {
            $res = db('share_log')->insert($insertData);
        } catch (\Exception $e) {
            err($e->getMessage());
        }
        
        suc();
    }
}

